package dao.impl;

import bean.dto.PermissionQueryPage;
import bean.pojo.Permissions;
import bean.vo.PermissionsOption;
import bean.vo.RouterVo;
import commons.jdbc.basic.JDBCUtil;
import dao.PermissionDao;

import java.util.*;

/**
 * dao.impl
 * User: mcw
 * Date: 2024/12/16 上午10:09
 * motto:   春天（spring）没有虫子（bug)
 * Description:
 * Version: V1.0
 */
public class PermissionDaoImpl implements PermissionDao {
    /**
     * 根据角色id查询该角色的所有一级菜单
     * @param roleId
     * @return
     */
    @Override
    public List<RouterVo> findRootByRoleId(Integer roleId) {
        String  sql="select   p.*  from   permissions   p  join  role_permissions rp  " +
                "on  p.permission_id=rp.permission_id  where  p.permission_parent_id is  null   and rp.role_id=? ";
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql, roleId);
        List<RouterVo>  list=new ArrayList<>();
        for (Map<String, Object> row : query) {
            RouterVo  routerVo=new RouterVo();
            routerVo.setId((Integer) row.get("permission_id"));
            routerVo.setTitle((String) row.get("permission_name"));
            routerVo.setName((String) row.get("permission_path"));
            routerVo.setIcon((String) row.get("permission_icon"));
            routerVo.setComponent((String) row.get("permission_web_url"));
            list.add(routerVo);
        }
        return list;
    }

    /**
     * 根据角色id和父菜单id查询该角色的所有子菜单
     * @param roleId
     * @param id
     * @return
     */
    @Override
    public List<RouterVo> findChildrenByRoleId(Integer roleId, Integer id) {
        // 根据父级Id和角色 查询这个角色拥有的这个子目录的 信息
        String  sql="select   p.*  from   permissions   p  join  role_permissions rp " +
                " on  p.permission_id=rp.permission_id  where  p.permission_parent_id =?   and rp.role_id=? ";
        JDBCUtil  jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql, id,roleId);
        List<RouterVo>  list=new ArrayList<>();
        for (Map<String, Object> row : query) {
            RouterVo  routerVo=new RouterVo();
            routerVo.setId((Integer) row.get("permission_id"));
            routerVo.setTitle((String) row.get("permission_name"));
            routerVo.setName((String) row.get("permission_path"));
            routerVo.setIcon((String) row.get("permission_icon"));
            routerVo.setComponent((String) row.get("permission_web_url"));
            list.add(routerVo);
        }
        return list;
    }
    /**
     * 查询记录数
     * @param permissionQueryPage
     * @return
     */
    @Override
    public Long findByTotal(PermissionQueryPage permissionQueryPage) {
        //sql
        StringBuffer   sql=new StringBuffer("select  count(*) ct  from  permissions  where  1=1 ");
        //集合参数
        List<Object> parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(permissionQueryPage.getPermissionName()!=null){
            parameters.add(permissionQueryPage.getPermissionName());
            sql.append(" and  permission_name like concat('%',?,'%') ");
        }
        if(permissionQueryPage.getPermissionParentId()!=null){
            parameters.add(permissionQueryPage.getPermissionParentId());
            sql.append(" and  permission_parent_id =? ");
        }
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(),parameters.toArray());
        //默认0条
        Long  ct=0L;
        if(query.size()>0){
            //该变
            ct=(Long)query.get(0).get("ct");
        }
        return ct;
    }

    /**
     * 分页查询
     * @param permissionQueryPage
     * @return
     */
    @Override
    public List<Permissions> findByPage(PermissionQueryPage permissionQueryPage) {
        //sql
        StringBuffer   sql=new StringBuffer("select * from permissions  where 1=1 ");
        //集合参数
        List<Object>  parameters=new ArrayList<>();
        //验证是否为空  null  "    "
        if(permissionQueryPage.getPermissionName()!=null){
            parameters.add(permissionQueryPage.getPermissionName());
            sql.append(" and  permission_name like concat('%',?,'%') ");
        }
        if(permissionQueryPage.getPermissionParentId()!=null){
            parameters.add(permissionQueryPage.getPermissionParentId());
            sql.append(" and  permission_parent_id =? ");
        }
        sql.append("  order by  permission_id desc");
        sql.append("  limit  "+((permissionQueryPage.getPage()-1)*permissionQueryPage.getLimit())
                +","+permissionQueryPage.getLimit());
        System.out.println("sql:"+sql);
        System.out.println("参数:"+parameters);
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), parameters.toArray());
        System.out.println(Arrays.toString(query.toArray()));
        if(query.size()>0){
            List<Permissions> list=new ArrayList<>();
            for (Map<String, Object> row : query) {//Community
                Permissions  permissions=new Permissions();
                permissions.setPermissionId((Integer) row.get("permission_id"));
                permissions.setPermissionName((String) row.get("permission_name"));
                permissions.setPermissionWebUrl((String) row.get("permission_web_url"));
                permissions.setPermissionJavaUrl((String) row.get("permission_java_url"));
                permissions.setPermissionParentId((Integer) row.get("permission_parent_id"));
                permissions.setPermissionIcon((String) row.get("permission_icon"));
                permissions.setPermissionPath((String) row.get("permission_path"));
                list.add(permissions);
            }
            System.out.println("List<permissions>:list:"+list);
            return  list;
        }
        return null;
    }

    /**
     * 查询所有父菜单信息
     * @return
     */
    @Override
    public List<PermissionsOption> findAllList() {
        String sql="select * from permissions  where permission_parent_id is null ";
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql);
        List<PermissionsOption> list=new ArrayList<>();
        for (Map<String, Object> row : query) {
            PermissionsOption  permissionsOption=new PermissionsOption();
            permissionsOption.setPermissionId((Integer) row.get("permission_id"));
            permissionsOption.setPermissionName((String) row.get("permission_name"));
            list.add(permissionsOption);
        }
        return list;
    }

    /**
     * 添加权限信息
     * @param permissions
     * @return
     */
    @Override
    public boolean insert(Permissions permissions) {
        String sql="insert into permissions(permission_name,permission_web_url,permission_java_url," +
                "permission_parent_id,permission_icon,permission_path) values(?,?,?,?,?,?)";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql, permissions.getPermissionName(), permissions.getPermissionWebUrl(), permissions.getPermissionJavaUrl(),
                permissions.getPermissionParentId(), permissions.getPermissionIcon(), permissions.getPermissionPath());
        System.out.println("sql:"+sql);
        return update > 0;
    }

    /**
     * 根据id删除权限信息
     * @param permissionId
     * @return
     */
    @Override
    public boolean deleteById(Integer permissionId) {
        String sql="delete from permissions where permission_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql, permissionId);
        return update>0;
    }

    /**
     * 修改权限信息
     * @param permissions
     * @return
     */
    @Override
    public boolean update(Permissions permissions) {
        String sql="update permissions set permission_name=?,permission_web_url=?,permission_java_url=?," +
                "permission_parent_id=?,permission_icon=?,permission_path=? where permission_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        int update = jdbcUtil.update(sql, permissions.getPermissionName(), permissions.getPermissionWebUrl(), permissions.getPermissionJavaUrl(),
                permissions.getPermissionParentId(), permissions.getPermissionIcon(), permissions.getPermissionPath(), permissions.getPermissionId());
        System.out.println("参数:"+permissions);
        System.out.println("sql:"+sql);
        return update>0;
    }

    /**
     * 查询所有菜单信息
     * @return
     */
    @Override
    public List<PermissionsOption> findAllPermissionList() {
        String sql="select * from permissions ";
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql);
        List<PermissionsOption> list=new ArrayList<>();
        for (Map<String, Object> row : query) {
            PermissionsOption  permissionsOption=new PermissionsOption();
            permissionsOption.setPermissionId((Integer) row.get("permission_id"));
            permissionsOption.setPermissionName((String) row.get("permission_name"));
            list.add(permissionsOption);
        }
        System.out.println("findAllPermissionList:sql:"+sql);
        System.out.println("findAllPermissionList:list:"+list);
        return list;
    }

    /**
     * 查询该角色拥有的菜单id
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> findCheckedPermissionList(Integer roleId) {
        String sql="select p.permission_id from permissions p left join role_permissions rp on p.permission_id=rp.permission_id where rp.role_id=?";
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql,roleId);
        List<Integer> list=new ArrayList<>();
        for (Map<String, Object> row : query) {
            Integer permissionId = (Integer) row.get("permission_id");
            list.add(permissionId);
        }
        System.out.println("findCheckedPermissionList:sql:"+sql);
        System.out.println("findCheckedPermissionList:list:"+list);
        return list;
    }
}
